package com.myMinistry.gui;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.myMinistry.R;
import com.myMinistry.provider.MinistryContract.EntryType;
import com.myMinistry.provider.MinistryContract.Literature;
import com.myMinistry.provider.MinistryContract.LiteratureType;
import com.myMinistry.provider.MinistryContract.Publisher;
import com.myMinistry.provider.MinistryService;

public class GettingStarted extends Activity {
	private MinistryService database;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.getting_started);
	    
	    database = new MinistryService(this);
	    
	    findViewById(R.id.createPublisher).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				EditText publisherName	= (EditText) findViewById(R.id.publisherName);
				/** Does the edit view have text? */
				if(publisherName.getText().length() > 0) {
					ContentValues values = new ContentValues();
					values.put(Publisher.NAME, publisherName.getText().toString());
					values.put(Publisher.ACTIVE, MinistryService.ACTIVE);
					
					database.openWritable();
					long publisherID = database.createPublisher(values);
					
					/** Create Default Entry Types */
					values = new ContentValues();
					values.put(EntryType.NAME, getApplicationContext().getString(R.string.rolloverTime));
					values.put(EntryType.ACTIVE, MinistryService.INACTIVE);
					values.put(EntryType.STUDY, MinistryService.INACTIVE);
					values.put(EntryType.RV, MinistryService.INACTIVE);
					values.put(EntryType.RBC, MinistryService.INACTIVE);
					values.put(EntryType.PUBLISHER_ID, (int) publisherID);
					database.createEntryType(values);

					values = new ContentValues();
					values.put(EntryType.NAME, getApplicationContext().getString(R.string.bibleStudy));
					values.put(EntryType.ACTIVE, MinistryService.ACTIVE);
					values.put(EntryType.STUDY, MinistryService.ACTIVE);
					values.put(EntryType.RV, MinistryService.INACTIVE);
					values.put(EntryType.RBC, MinistryService.INACTIVE);
					values.put(EntryType.PUBLISHER_ID, (int) publisherID);
					database.createEntryType(values);

					values = new ContentValues();
					values.put(EntryType.NAME, getApplicationContext().getString(R.string.returnVisit));
					values.put(EntryType.ACTIVE, MinistryService.ACTIVE);
					values.put(EntryType.STUDY, MinistryService.INACTIVE);
					values.put(EntryType.RV, MinistryService.ACTIVE);
					values.put(EntryType.RBC, MinistryService.INACTIVE);
					values.put(EntryType.PUBLISHER_ID, (int) publisherID);
					database.createEntryType(values);

					values = new ContentValues();
					values.put(EntryType.NAME, getApplicationContext().getString(R.string.ministryService));
					values.put(EntryType.ACTIVE, MinistryService.ACTIVE);
					values.put(EntryType.STUDY, MinistryService.INACTIVE);
					values.put(EntryType.RV, MinistryService.INACTIVE);
					values.put(EntryType.RBC, MinistryService.INACTIVE);
					values.put(EntryType.PUBLISHER_ID, (int) publisherID);
					database.createEntryType(values);

					values = new ContentValues();
					values.put(EntryType.NAME, getApplicationContext().getString(R.string.rebuildCommittee));
					values.put(EntryType.ACTIVE, MinistryService.ACTIVE);
					values.put(EntryType.STUDY, MinistryService.INACTIVE);
					values.put(EntryType.RV, MinistryService.INACTIVE);
					values.put(EntryType.RBC, MinistryService.ACTIVE);
					values.put(EntryType.PUBLISHER_ID, (int) publisherID);
					database.createEntryType(values);
					
					/** Create Default Literature Types */
					values = new ContentValues();
					values.put(LiteratureType.NAME, getApplicationContext().getString(R.string.books));
					values.put(LiteratureType.ACTIVE, MinistryService.ACTIVE);
					values.put(LiteratureType.PUBLISHER_ID, (int) publisherID);
					values.put(LiteratureType.SORT_ORDER, 0);
					database.createLiteratureType(values);
					
					values = new ContentValues();
					values.put(LiteratureType.NAME, getApplicationContext().getString(R.string.booklets));
					values.put(LiteratureType.ACTIVE, MinistryService.ACTIVE);
					values.put(LiteratureType.PUBLISHER_ID, (int) publisherID);
					values.put(LiteratureType.SORT_ORDER, 1);
					database.createLiteratureType(values);
					
					values = new ContentValues();
					values.put(LiteratureType.NAME, getApplicationContext().getString(R.string.magazines));
					values.put(LiteratureType.ACTIVE, MinistryService.ACTIVE);
					values.put(LiteratureType.PUBLISHER_ID, (int) publisherID);
					values.put(LiteratureType.SORT_ORDER, 2);
					database.createLiteratureType(values);
					
					values = new ContentValues();
					values.put(LiteratureType.NAME, getApplicationContext().getString(R.string.media));
					values.put(LiteratureType.ACTIVE, MinistryService.ACTIVE);
					values.put(LiteratureType.PUBLISHER_ID, (int) publisherID);
					values.put(LiteratureType.SORT_ORDER, 3);
					database.createLiteratureType(values);
					
					/** Create Default Literature */
					values = new ContentValues();
					values.put(Literature.NAME, getApplicationContext().getString(R.string.awake));
					values.put(Literature.ACTIVE, MinistryService.ACTIVE);
					values.put(Literature.PUBLISHER_ID, (int) publisherID);
					values.put(Literature.SORT_ORDER, 1);
					values.put(Literature.TYPE_OF_LIERATURE_ID, 3);
					values.put(Literature.WEIGHT, 1);
					database.createLiterature(values);

					values = new ContentValues();
					values.put(Literature.NAME, getApplicationContext().getString(R.string.watchtower));
					values.put(Literature.ACTIVE, MinistryService.ACTIVE);
					values.put(Literature.PUBLISHER_ID, (int) publisherID);
					values.put(Literature.SORT_ORDER, 1);
					values.put(Literature.TYPE_OF_LIERATURE_ID, 3);
					values.put(Literature.WEIGHT, 1);
					database.createLiterature(values);

					values = new ContentValues();
					values.put(Literature.NAME, getApplicationContext().getString(R.string.watchtowerAndAwake));
					values.put(Literature.ACTIVE, MinistryService.ACTIVE);
					values.put(Literature.PUBLISHER_ID, (int) publisherID);
					values.put(Literature.SORT_ORDER, 1);
					values.put(Literature.TYPE_OF_LIERATURE_ID, 3);
					values.put(Literature.WEIGHT, 2);
					database.createLiterature(values);
					database.close();
					
					/** Get Preferences */
					SharedPreferences sharedPref = getSharedPreferences(Summary.MY_PREFS, MODE_PRIVATE);
					SharedPreferences.Editor editor = sharedPref.edit();
					/** Set values */
					editor.putBoolean(Summary.MY_PREFS_KEY_CREATED, true);
					editor.putInt(Summary.MY_PREFS_KEY_PUBLISHER_ID, (int) publisherID);
					editor.putString(Summary.MY_PREFS_KEY_PUBLISHER_NAME, publisherName.getText().toString());
					/** Defaulting to not show the "what's new" section */
					try {
						editor.putInt(Summary.VERSION_KEY, getPackageManager().getPackageInfo(getPackageName(), 0).versionCode);
					} catch (Exception e) {}
					
					/** Commit Preferences */
					editor.commit();
					
					/** Now lets go to the summary default home page! */
					Intent intent = new Intent(getApplicationContext(), Summary.class);
					startActivity(intent);
					finish();
				}
				
				/** Failure */
				else
					publisherName.setError(getText(R.string.formNameError));
			}
		});
	}
}